package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.ShoppingCart;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public JsonResult add(ShoppingCart shoppingCart) {

        //判断是菜品还是套餐
        if (shoppingCart.getSetmealId() != null) {
            ShoppingCart one = getOne((Wrapper<ShoppingCart>) new QueryWrapper().eq("setmeal_id", shoppingCart.getSetmealId()));
            if (one == null) {
                //如果不存在，第一次存进去
                save(shoppingCart);
            } else {
                //如果存在,更新数量+1
                one.setNumber(one.getNumber() + 1);
                updateById(one);
            }
        } else {
            ShoppingCart one = getOne((Wrapper<ShoppingCart>) new QueryWrapper().eq("dish_id", shoppingCart.getDishId()));
            if (one == null) {
                save(shoppingCart);
            } else {
                one.setNumber(one.getNumber() + 1);
                updateById(one);
            }
        }
        return new JsonResult(1, null, shoppingCart, null);
    }

    /**
     * 清空购物车
     * @param
     * @return
     */
    @Override
    public JsonResult delete() {
        //删除所有
        remove(null);

        return JsonResult.success("清空购物车成功");
    }

    /**
     * 购物车列表
     * @return
     */
    @Override
    public JsonResult shoppingCartList() {
        //查询所有
        List<ShoppingCart> list = list();

        return JsonResult.success(list);
    }

    /**
     * 删除购物车商品
     * @param shoppingCart
     * @return
     */
    @Override
    public JsonResult deleteById(ShoppingCart shoppingCart) {
        //判断是菜品还是套餐
        if (shoppingCart.getDishId() == null) {
            ShoppingCart one = getOne((Wrapper<ShoppingCart>)
                    new QueryWrapper().eq("setmeal_id", shoppingCart.getSetmealId()));
            //如果数量大于1，number-1      等于1，删除整条
            if (one.getNumber() == 1) {
                removeById(one);
            } else {
                one.setNumber(one.getNumber() - 1);
                updateById(one);
            }
        } else {
            ShoppingCart one = getOne((Wrapper<ShoppingCart>)
                    new QueryWrapper().eq("dish_id", shoppingCart.getDishId()));

            if (one.getNumber() == 1) {
                removeById(one);
            } else {
                one.setNumber(one.getNumber() - 1);
                updateById(one);
            }
        }
        return new JsonResult(1, null, "删除购物车商品成功",null);
    }
}
